package com.phiture.erp.produce.web.controller.admin;

import com.phiture.erp.produce.core.pojo.vo.ProduceInEventPageReqVO;
import com.phiture.erp.produce.core.pojo.vo.ProduceInEventRespVO;
import com.phiture.erp.produce.core.pojo.vo.ProduceInEventSaveReqVO;
import com.phiture.erp.produce.core.service.ErpProduceInEventService;
import com.phiture.erp.produce.dal.entity.ErpProduceInEventDO;
import com.phiture.erp.produce.dal.entity.ErpProduceInEventItemsDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 生产入库事务")
@RestController
@RequestMapping("/erp/produce-in-event")
@Validated
public class ErpProduceInEventController {

    @Resource
    private ErpProduceInEventService produceInEventService;

    @PostMapping("/create")
    @Operation(summary = "创建生产入库事务")
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:create')")
    public CommonResult<Long> createProduceInEvent(@Valid @RequestBody ProduceInEventSaveReqVO createReqVO) {
        return success(produceInEventService.createProduceInEvent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产入库事务")
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:update')")
    public CommonResult<Boolean> updateProduceInEvent(@Valid @RequestBody ProduceInEventSaveReqVO updateReqVO) {
        produceInEventService.updateProduceInEvent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产入库事务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:delete')")
    public CommonResult<Boolean> deleteProduceInEvent(@RequestParam("id") Long id) {
        produceInEventService.deleteProduceInEvent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产入库事务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:query')")
    public CommonResult<ProduceInEventRespVO> getProduceInEvent(@RequestParam("id") Long id) {
        ErpProduceInEventDO produceInEvent = produceInEventService.getProduceInEvent(id);
        return success(BeanUtils.toBean(produceInEvent, ProduceInEventRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产入库事务分页")
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:query')")
    public CommonResult<PageResult<ProduceInEventRespVO>> getProduceInEventPage(@Valid ProduceInEventPageReqVO pageReqVO) {
        PageResult<ErpProduceInEventDO> pageResult = produceInEventService.getProduceInEventPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProduceInEventRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产入库事务 Excel")
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProduceInEventExcel(@Valid ProduceInEventPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProduceInEventDO> list = produceInEventService.getProduceInEventPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产入库事务.xls", "数据", ProduceInEventRespVO.class,
                        BeanUtils.toBean(list, ProduceInEventRespVO.class));
    }

    // ==================== 子表（生产入库事务明细） ====================

    @GetMapping("/produce-in-event-items/list-by-parent-id")
    @Operation(summary = "获得生产入库事务明细列表")
    @Parameter(name = "parentId", description = "父ID")
    @PreAuthorize("@ss.hasPermission('erp:produce-in-event:query')")
    public CommonResult<List<ErpProduceInEventItemsDO>> getProduceInEventItemsListByParentId(@RequestParam("parentId") Long parentId) {
        return success(produceInEventService.getProduceInEventItemsListByParentId(parentId));
    }

}